/**
 * Created by zhoulin on 2019/4/3.
 */
var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);

var app = new Vue({
    el: '#app',
    data: {
        permissions:['companyCooperateApply:add','companyCooperateApply:update'],//本页权限数组
        selects:[],
        desc:'',
        schoolId:schoolId,
        schoolList:schoolMap,
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if(empty(token)){
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon:5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            },1200);
            return ;
        }
    },
    methods: {
        // 权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        selectCompany: function() {
            var url = "./company.html";
            var index = layer.open({
                type: 2,
                title: "选择公司",
                content: url,
                fix: true,
                shadeClose: true,
                shade: false,
                area: ['70%', '70%'],
                maxmin: true, //开启最大化最小化按钮
                end:function(){//关闭后调用的事件
                    var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.selectOp.objectId);
                    var companyName = sessionStorageUtils.getJson(storageKey.sessionStorageKey.selectOp.objectName);
                    $("#Form input[name='cooperateCompanyId']").val(companyId);
                    $("#Form input[name='companyName']").val(companyName);
                    sessionStorageUtils.del(storageKey.sessionStorageKey.selectOp.objectId);
                    sessionStorageUtils.del(storageKey.sessionStorageKey.selectOp.objectName);
                }
            });
            layer.full(index);
        },
        // 添加
        add:function () {
            var that = this;
            var param = formToJson($("#Form").serialize());
            param['companyId'] = companyId;
            param['schoolId'] = schoolId;
            //console.log(param)
            Post(that,_HOST+_API.product_cooperateApply_add,param,true,that.permissions[0])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            layer.msg(response.msg, {time: 1500, icon:1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                    $("#Form").data('bootstrapValidator').destroy();
                    $('#Form').data('bootstrapValidator', null);
                    Validator();
                })
        },
    },
});

$('#Form').data('bootstrapValidator', null);
Validator();
function Validator() {
    $('#Form').bootstrapValidator({
        fields: {
            type: {
                validators: {
                    notEmpty: {
                        message: '请选择类型'
                    },
                }
            },
        }
    }).on('success.form.bv', function (e) {
        // Prevent form submission
        e.preventDefault();
        if(empty(aGET.id)){
            app.add();
        }
    });
}
